package com.ithinks.flow.common.utils.protocol.socket.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description
 * @ClassName BioServer
 * @Author MR.LI
 * @date 2020.12.08 16:56
 */
public class BioServer {

    //服务器端必须
    private static ServerSocket server;
    //
    private static ExecutorService executorService
            = Executors.newFixedThreadPool(5);
    private static void start(){
        try {
            server = new ServerSocket(222);
            System.out.println("服务端已启动，端口号："+222);
            while(true){
                Socket socket = server.accept();
                System.out.println("有新的连接---");
                executorService.execute(new BioServerHandler(socket));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(server != null){
                try {
                    server.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        start();
    }
}
